use std::fs::File;
use std::io::{self, prelude::*, BufReader};

fn main() -> io::Result<()>{

    let input = File::open("input.txt")?;
    let reader = BufReader::new(input);
    let plane_size = 1 << (3 + // 3 columns
                           7); // 7 rows
    let mut free_seats: Vec<bool> = vec![true; plane_size];
    
    for line in reader.lines() {
        let str = line?;
        let mut seat: usize = 0;
        let str_bound = str.len() - 1;
        for (i, c) in str.chars().enumerate(){
             match c {
                'B'|'R' => {
                    seat |= 1 << (str_bound - i);

                },
                'F'|'L' => {},
                _ => {},
             }
        }
        free_seats[seat] = false;
        
    }
    let mut highest_reserved_id : i32 = -1;
    for (i, free) in free_seats.iter().rev().enumerate(){
        let seat_id = plane_size - 1 - i;
        if !*free && highest_reserved_id == -1 {
            highest_reserved_id = seat_id as i32;
            println!("Part 1: Highest reserved seat is {}", highest_reserved_id);
        }else if *free && (seat_id as i32) < highest_reserved_id{
            println!("Part 2: My seat is {}", seat_id);
            break;
        }
    }
    Ok(())
}
